package com.yDev.imagemaker;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class mCusView extends View {

	Paint paint;
	ArrayList<Point> points;
	Point newPoint=new Point(0,0);
	Point oldPoint= new Point(0, 0);
	
	ArrayList<Path> paths;
	ArrayList<Integer> colors;
	
    private Paint fillPaint;  
    //Create a paint for the stroke  
    private Paint strokePaint;  
  
    //A Bitmap object that is going to be passed to the BitmapShader  
    private Bitmap fillBMP;  
    //The shader that renders the Bitmap  
    private BitmapShader fillBMPshader;  
  
    //A matrix object  
    private Matrix m = new Matrix();  
  
    //Two floats to store the touch position  
    private float posX = 105;  
    private float posY = 105;
	
	public mCusView(Context context) {
		super(context);
		paint=new Paint();
		points=new ArrayList<Point>();
		
		paths= new ArrayList<Path>();
		colors = new ArrayList<Integer>();
		
		
		setBackgroundColor(Color.WHITE);
 
		  
        //This View can receive focus, so it can react to touch events.  
        this.setFocusable(true);  
  
        //Initialize the strokePaint object and define some of its parameters  
        strokePaint = new Paint();  
        strokePaint.setDither(true);  
        strokePaint.setColor(0xFFFFFF00);  
        strokePaint.setStyle(Paint.Style.STROKE);  
        strokePaint.setAntiAlias(true);  
        strokePaint.setStrokeWidth(3);  
  
        //Initialize the bitmap object by loading an image from the resources folder  
        fillBMP = BitmapFactory.decodeResource(context.getResources(), R.drawable.rock_texture1);  
        //Initialize the BitmapShader with the Bitmap object and set the texture tile mode  
        fillBMPshader = new BitmapShader(fillBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);  
  
        //Initialize the fillPaint object  
        fillPaint = new Paint();  
        fillPaint.setColor(0xFFFFFFFF);  
        fillPaint.setStyle(Paint.Style.FILL);  
        //Assign the 'fillBMPshader' to this paint  
        fillPaint.setShader(fillBMPshader);  
	
	}

	public mCusView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public mCusView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	

	
    
 
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);

		paint.setStrokeWidth(10);
		paint.setColor(Color.BLACK);
		//Paint paint = new Paint();
/*		for(int i =0; i < paths.size() ;i++){ 
		//paint.setColor(colors.get(i));
		canvas.drawPath(paths.get(i), paint);
		}*/

		
		canvas.getMatrix().invert(m);  
        //Assign the matrix to the BitmapShader. Again, not required to make this example work.  
        fillBMPshader.setLocalMatrix(m);  
  
        //Draw the fill  
        canvas.drawCircle(posX, posY, 100, fillPaint);  
        //Afterwards, draw the circle again, using the stroke paint  
        //canvas.drawCircle(posX, posY, 100, strokePaint);  
		
        
        for(int i =0; i < paths.size() ;i++){ 
    		//paint.setColor(colors.get(i));
    		canvas.drawPath(paths.get(i), fillPaint);
    		}
	}
	Path path = new Path();
	int i=0;
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		
/*		oldPoint=new Point(newPoint);
		newPoint= new Point((int) event.getX(), (int) event.getY());

		
		Path path = new Path();
		if(paths.size()==0)
	      path.moveTo(event.getX(), event.getY());
	      path.lineTo(event.getX(), event.getY());
		paths.add(path);
		
		//colors.add(new Integer(paint.getColor()))
		//points.add(new Point((int) event.getX(), (int) event.getY()));
		invalidate();
		return true;*/
		paint.setStyle(Style.STROKE);
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            path = new Path();
            path.moveTo(event.getX(), event.getY());
            path.lineTo(event.getX(), event.getY());
            paths.add(i, path);
        }else if(event.getAction() == MotionEvent.ACTION_MOVE){
            path.lineTo(event.getX(), event.getY());
            paths.add(i, path);
        }else if(event.getAction() == MotionEvent.ACTION_UP){
            path.lineTo(event.getX(), event.getY());
            paths.add(i, path);
            i++;
        }
        invalidate();
        return true;
	
	}

	public void setColor(int color) {
		paint.setColor(color);
		
	}

}
